package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 登录控制
 */
@Controller
public class LoginController {
    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request,HttpServletResponse response,String username,String password) throws IOException {
        if("admin".equals(username) && "admin".equals(password)){
            HttpSession session = request.getSession();
            session.setAttribute("loginUser","admin");
            System.out.println("用户登录成功");
            return "redirect:list";
        }else {
            request.setAttribute("error","用户名或密码错误，请重新输入");
            return "forward:index.jsp";
        }

    }


    @RequestMapping("/list")
    public ModelAndView jumpToList(ModelAndView modelAndView) {
        List<Resume> resumes = resumeService.queryAllResume();
        modelAndView.addObject("resumes",resumes);
        modelAndView.setViewName("list");
        return modelAndView;
    }
}
